<!-- 商品类型管理界面 -->
<%@ page language="java" pageEncoding="UTF-8" contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<script type="text/javascript">
var treegrid;
	$(function() {
		treegrid=$('#admin_spgl_treegrid').treegrid({
			url : '${pageContext.request.contextPath}/goodTypesAction!treegrid.action',
			idField : 'id',
			treeField : 'name',
			parentField : 'pid',
			fit : true,
			fitColumns : true,
			border : false,
			frozenColumns : [ [ {
				title : '编号',
				field : 'id',
				width : 150,
				hidden : true
			}, {
				field : 'name',
				title : '类型名称',
				width : 200
			} ] ],
			columns : [ [ {
				field : 'pid',
				title : '上级类型ID',
				width : 150,
				hidden : true
			}, {
				field : 'pname',
				title : '上级类型名称',
				width : 80
			}] ],
			toolbar :'#toolbar',
			onContextMenu : function(e, row) {
				e.preventDefault();
				$(this).treegrid('unselectAll');
				$(this).treegrid('select', row.id);
				$('#admin_splxgl_menu').menu('show', {
					left : e.pageX,
					top : e.pageY
				});
			}
		});
    spreadFun=function(){
    	var node = $('#admin_spgl_treegrid').treegrid('getSelected');
		if (node) {
			$('#admin_spgl_treegrid').treegrid('expandAll', node.cid);
		} else {
			$('#admin_spgl_treegrid').treegrid('expandAll');
		}
    };
    foldFun=function(){
    	var node = $('#admin_spgl_treegrid').treegrid('getSelected');
		if (node) {
			$('#admin_spgl_treegrid').treegrid('collapseAll', node.cid);
		} else {
			$('#admin_spgl_treegrid').treegrid('collapseAll');
		}
    };
    refresh=function(){
    	$('#admin_spgl_treegrid').treegrid('reload');
    };
	editFun=function(id) {
		if (id != undefined) {
			treegrid.treegrid('select', id);
		}
		var node = treegrid.treegrid('getSelected');
		/* var rows = treegrid.treegrid('getSelections'); */
		/* console.info(node); */
		if(node!=null){
		if(node.id!=0){
			var p = parent.dialog({
				title : '编辑商品类型',
				href : '${pageContext.request.contextPath}/goodTypesAction!goodTypesEdit.action',
				width : 300,
				height : 200,
				buttons : [ {
					text : '编辑',
					iconCls :'icon-edit',
					handler : function() {
						var f = p.find('form');
						f.form('submit', {
							url : '${pageContext.request.contextPath}/goodTypesAction!editor.action',
							success : function(d) {
								var json = $.parseJSON(d);
								if (json.success) {
									treegrid.treegrid('unselectAll');
									treegrid.treegrid('reload');
									p.dialog('close');
								}
								parent.messagerShow({
									msg : json.msg,
									title : '提示'
								});
							}
						});
					}
				} ],
				onLoad : function() {
					var f = p.find('form');
					f.form('load', {
						id : node.id,
						name : node.name,
						typeid : node.pname
					});
				}
			});
		}else{
			 $.messager.alert('警告', '不能对根节点进行编辑！', 'warning');
		}
	  }else{
		     $.messager.alert('警告', '请选择要编辑的记录！', 'warning');
	  }
	};
	delFun=function() {
		var node = treegrid.treegrid('getSelected');
		if (node) {
			if (node.id == '0') {
				$.messager.alert('警告', '不能删除根节点！', 'warning');
				return;
			}
			$.messager.confirm('询问', '您确定要删除该类型？', function(b) {
				if (b) {
					$.ajax({
						url : '${pageContext.request.contextPath}/goodTypesAction!del.action',
						data : {
							id : node.id
						},
						cache : false,
						dataType : "json",
						success : function(r) {
							if (r.success) {
								treegrid.treegrid('reload');
								treegrid.treegrid('unselectAll');
								$.messager.show({
									msg : r.msg,
									title : '提示'
								});
							} else {
								$.messager.show({
									msg : '删除失败!',
									title : '提示'
								});
							}
						}
					});
				}
			});
		}else{
			$.messager.alert('警告', '请选择要删除的记录！', 'warning');
		}
	};
	addFun=function() {
		var p = parent.dialog({
			title : '添加商品类型',
			href : '${pageContext.request.contextPath}/goodTypesAction!goodTypesAdd.action',
			width : 300,
			height : 200,
			buttons : [ {
				text : '添加',
				iconCls :'icon-add',
				handler : function() {
					var f = p.find('form');
					f.form('submit', {
						url : '${pageContext.request.contextPath}/goodTypesAction!add.action',
						success : function(d) {
							var json = $.parseJSON(d);
							if (json.success) {
								treegrid.treegrid('reload');
								treegrid.treegrid('unselectAll');
								p.dialog('close');
							}
							parent.messagerShow({
								msg : json.msg,
								title : '提示'
							});
						}
					});
				}
			} ]
		    
		});
	};
});
</script>
<table id="admin_spgl_treegrid"></table>
<div id="admin_splxgl_menu" class="easyui-menu" style="width:120px;display: none;">
<shiro:hasPermission name="sys:splx:edit">
	<div onclick="editFun();" data-options="iconCls:'icon-edit'">编辑</div>
</shiro:hasPermission>
</div>
<div id="toolbar" style="display: none;">
<table>
			<tr>
				<shiro:hasPermission name="sys:splx:add">
				<td><a href="javascript:void(0);" class="easyui-linkbutton" data-options="iconCls:'icon-add',plain:true" onclick="addFun();">添加</a></td>
				</shiro:hasPermission>
				<shiro:hasPermission name="sys:splx:del">
				<td><a href="javascript:void(0);" class="easyui-linkbutton" data-options="iconCls:'icon-remove',plain:true" onclick="delFun();">删除</a></td>
				</shiro:hasPermission>
				<shiro:hasPermission name="sys:splx:edit">
				<td><a href="javascript:void(0);" class="easyui-linkbutton" data-options="iconCls:'icon-edit',plain:true" onclick="editFun();">修改</a></td>
				</shiro:hasPermission>
				<td><a href="javascript:void(0);" class="easyui-linkbutton" data-options="iconCls:'icon-redo',plain:true" onclick="spreadFun();">展开</a></td>
				<td><a href="javascript:void(0);" class="easyui-linkbutton" data-options="iconCls:'icon-undo',plain:true" onclick="foldFun();">折叠</a></td>
				<td><a href="javascript:void(0);" class="easyui-linkbutton" data-options="iconCls:'icon-reload',plain:true" onclick="refresh();">刷新</a></td>
			</tr>
</table>
</div>